﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "计划报表";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}
@section Contents{
    <link href="~/res/kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="~/res/kendo/styles/kendo.material.min.css" rel="stylesheet" />
    <style>
        .layui-form-label {
            width: auto;
        }

        #txtOrderNo, #txtWorkOrderNo {
            width: 100%;
            height: 30px;
        }

        .layui-form-item .layui-form-checkbox[lay-skin="primary"] {
            margin-top: -5px !important;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" placeholder="请输入订单号" value="@ViewBag.OrderNo" @(!string.IsNullOrWhiteSpace(ViewBag.OrderNo) ? " disabled " : "") autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" placeholder="请输入工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="checkbox" id="cbLeverPowers" title="各等级功率分布" lay-skin="primary" style="margin-top: -5px;">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderDate">生产时间：</label>
                        <div class="layui-input-inline" style="width:440px;">
                            <input type="text" id="txtRangeTime" style="width:310px;float:left;" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnPreDuty" type="button" style="margin-top:-6px;">
                                <i class="layui-icon layui-icon-prev layuiadmin-button-btn"></i>
                            </button>
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnNextDuty" type="button" style="margin-top:-6px;margin-left:0;">
                                <i class="layui-icon layui-icon-next layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" style="margin-top:-6px;" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <a id="btnExport" class="layui-btn layui-btn-normal layuiadmin-btn-list" style="margin-top:-6px;" target="_blank" href="javascript:void(0)">
                            <i class="layui-icon layui-icon-download-circle layuiadmin-button-btn"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/kendo/kendo.all.min.js"></script>
    <script src="/res/kendo/js/cultures/kendo.culture.zh-CN.min.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeTime'
                    , range: '至'
                    , type: 'datetime'
                    , done: function (value, date, endDate) {
                        queryNos(0);
                        queryNos(1);
                    }
                });
                form.on("select(selWorkshops)", function (data) {
                    queryNos(0);
                    queryNos(1);
                });

                var txtOrderNos = $("#txtOrderNo").kendoAutoComplete({
                    dataSource: vm.$data.nos,
                    filter: "contains",
                    placeholder: "选择订单号"
                }).focus(function () {
                    txtOrderNos.search("");
                }).data("kendoAutoComplete"),
                    txtWorkOrderNos = $("#txtWorkOrderNo").kendoAutoComplete({
                        dataSource: vm.$data.nos,
                        filter: "contains",
                        placeholder: "选择工单号"
                    }).focus(function () {
                        txtWorkOrderNos.search("");
                    }).data("kendoAutoComplete");

                function queryNos(noType) {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0],
                        endTime = rangeTime.length <= 0 ? null : rangeTime[1];

                    $.ajax({
                        url: "/Common/QueryOrderNos?noType=" + noType + "&workshopCode=" + $("#selWorkshops").val().trim() + "&key=" + "&startTime=" + startTime + "&endTime=" + endTime,
                        type: "GET",
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            if (noType == 0) {
                                txtOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            } else {
                                txtWorkOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            }
                        }
                    });
                }
                var options = {
                    elem: '#table'
                    , data: []
                    , limit: 9999
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0];
                    var endTime = rangeTime.length <= 0 ? null : rangeTime[1];

                    var loadId = layer.load();
                    $.ajax({
                        url: '/Report/QueryStatistics',
                        data: {
                            workshopCode: $("#selWorkshops").val().trim(),
                            orderNo: $("#txtOrderNo").val().trim(),
                            workOrderNo: $("#txtWorkOrderNo").val().trim(),
                            needLevelPowers: $("#cbLeverPowers")[0].checked,
                            startTime: startTime,
                            endTime: endTime
                        },
                        dataType: 'json',
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                return layer.msg(res.ErrMsg, { icon: 5 });
                            }

                            options.cols = [[{ title: '序号', type: "numbers", width: 60 }]];
                            for (var i = 0; i < res.Data.Headers.length; i++) {
                                options.cols[0].push({ field: res.Data.Headers[i], title: res.Data.Headers[i], minWidth: 150 });
                            }

                            options.data = res.Data.Data;
                            if (table.index <= 0) {
                                table.render(options);
                            } else {
                                table.reload("table", options);
                            }
                        }
                    });
                }

                $("#btnExport").click(function () {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0];
                    var endTime = rangeTime.length <= 0 ? null : rangeTime[1];
                    var workshopCode = $("#selWorkshops").val().trim(),
                        orderNo = $("#txtOrderNo").val().trim(),
                        workOrderNo = $("#txtWorkOrderNo").val().trim();
                    $(this).attr("href", "/Report/ExportStatistics?workshopCode=" + workshopCode + "&orderNo=" + orderNo + "&workOrderNo=" + workOrderNo + "&needLevelPowers=" + $("#cbLeverPowers")[0].checked + "&startTime=" + startTime + "&endTime=" + endTime);
                });
            });
        }
    </script>
}
